﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Entities.Polygon;
using PPI.WebConsumer.Polygon;
using VMind.Global;

namespace PPI.Client.Logic.Polygon
{
    public class LPolygon
    {
        public static List<EUtility_Polygon> GetAll()
        {
            DataTable dt = WPolygon.SelectAll().Tables[0];
            return Parse(dt);
        }

        private static List<EUtility_Polygon> Parse(DataTable dt)
        {
            Int32 polygonId = Int32.MinValue;
            Int32 currentpolygonId = Int32.MinValue;

            List<EUtility_Polygon> listPolygons = new List<EUtility_Polygon>();
            List<EUtility_Polygon_Point> polygonPoints = new List<EUtility_Polygon_Point>();
            EUtility_Polygon_Point polygonPoint = null;
            EUtility_Polygon polygon = null;

            DataRow dr = null;
            DataRow previewsRow = null;

            for(Int32 i = 0; i<dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                if (i != 0)
                    previewsRow = dt.Rows[i - 1];

                currentpolygonId = ConvertHelper.ToInt32(dr["Utility_PolygonId"]);
                if (polygonId != Int32.MinValue)
                {
                    if (currentpolygonId != polygonId)
                    {
                        polygonId = currentpolygonId;

                        polygon = new EUtility_Polygon();
                        polygon.Id = ConvertHelper.ToInt32(previewsRow["Utility_PolygonId"]);
                        polygon.Name = previewsRow["Name"].ToString();
                        polygon.Altitude = ConvertHelper.Todouble(previewsRow["Altitude"]);
                        polygon.North = ConvertHelper.Todouble(previewsRow["North"]);
                        polygon.East = ConvertHelper.Todouble(previewsRow["East"]);
                        polygon.South = ConvertHelper.Todouble(previewsRow["South"]);
                        polygon.West = ConvertHelper.Todouble(previewsRow["West"]);
                        polygon.LineColor = ConvertHelper.ToInt32(previewsRow["LineColor"]);
                        polygon.LineWidth = ConvertHelper.ToInt32(previewsRow["LineWidth"]);
                        polygon.FillColor = ConvertHelper.ToInt32(previewsRow["FillColor"]);
                        polygon.FillOpacity = ConvertHelper.ToInt32(previewsRow["FillOpacity"]);
                        polygon.FontColor = ConvertHelper.ToInt32(previewsRow["FontColor"]);
                        polygon.Font = previewsRow["Font"].ToString();
                        polygon.Utility_Polygon_Points = polygonPoints;
                        listPolygons.Add(polygon);
                        polygonPoints = new List<EUtility_Polygon_Point>();
                    }
                }

                polygonPoint = new EUtility_Polygon_Point();
                polygonPoint.Latitude = ConvertHelper.Todouble(dr["Latitude"]);
                polygonPoint.Longitude = ConvertHelper.Todouble(dr["Longitude"]);
                polygonPoint.Utility_Polygon_PointId = ConvertHelper.ToInt32(dr["Utility_Polygon_PointId"]);
                polygonPoint.Utility_Polygon = new EUtility_Polygon(ConvertHelper.ToInt32(dr["Utility_PolygonFK"]));
                polygonPoints.Add(polygonPoint);

                if (polygonId == Int32.MinValue)
                    polygonId = currentpolygonId;

            }
            if(dt.Rows.Count > 0)
            {
                previewsRow = dt.Rows[dt.Rows.Count - 1];
                polygon = new EUtility_Polygon();
                polygon.Id = ConvertHelper.ToInt32(previewsRow["Utility_PolygonId"]);
                polygon.Name = previewsRow["Name"].ToString();
                polygon.Altitude = ConvertHelper.Todouble(previewsRow["Altitude"]);
                polygon.North = ConvertHelper.Todouble(previewsRow["North"]);
                polygon.East = ConvertHelper.Todouble(previewsRow["East"]);
                polygon.South = ConvertHelper.Todouble(previewsRow["South"]);
                polygon.West = ConvertHelper.Todouble(previewsRow["West"]);
                polygon.LineColor = ConvertHelper.ToInt32(previewsRow["LineColor"]);
                polygon.LineWidth = ConvertHelper.ToInt32(previewsRow["LineWidth"]);
                polygon.FillColor = ConvertHelper.ToInt32(previewsRow["FillColor"]);
                polygon.FillOpacity = ConvertHelper.ToInt32(previewsRow["FillOpacity"]);
                polygon.FontColor = ConvertHelper.ToInt32(previewsRow["FontColor"]);
                polygon.Font = previewsRow["Font"].ToString();
                polygon.Utility_Polygon_Points = polygonPoints;
                listPolygons.Add(polygon);
                polygonPoints = new List<EUtility_Polygon_Point>();
            }


            return listPolygons;
        }


        public static void GetCoordinatesLimits(List<EUtility_Polygon> polygons, ref double maxLongitude, ref double maxLatitude, ref double minLongitude, ref double minLatitude)
        {
            maxLatitude = double.MinValue;
            maxLongitude = double.MinValue;
            minLatitude = double.MaxValue;
            minLongitude = double.MaxValue;
            foreach (EUtility_Polygon polygon in polygons)
            {
                foreach (EUtility_Polygon_Point points in polygon.Utility_Polygon_Points)
                {
                    if (points.Latitude > maxLatitude)
                        maxLatitude = points.Latitude;
                    if (points.Longitude > maxLongitude)
                        maxLongitude = points.Longitude;
                    if (points.Latitude < minLatitude)
                        minLatitude = points.Latitude;
                    if (points.Longitude < minLongitude)
                        minLongitude = points.Longitude;
                }
            }
        }
    }
}
